var http = require('http')
var url =require('url')
var querystring = require('querystring')

function serevrStart (route,handle){
    var onRequest = function(req,res){
        var pathname = url.parse(req.url).pathname
        var data = []
        req.on('error',function(err){
            console.error(err)
        }).on('data',function(chunk){
            data.push(chunk)
        }).on('end',function(){
            if(req.method === 'POST'){
                data = Buffer.concat(data).toString();
                route(handle,res,pathname,querystring.parse(data))
            }else {
                var params = url.parse(req.url,true).query
                route(handle,res,pathname,params)
            }
        })
    }

    var server = http.createServer(onRequest)

    server.listen(2020,'127.0.0.1')
    console.log('server started')
}
module.exports = {
    serevrStart
}